import time
from typing import Generic, TypeVar, Any, Dict, List

from pydantic import BaseModel, Field

from common.result import result_code
from common.result.err_msg import ErrMsg, Success

T = TypeVar('T')


class ResultMsg(BaseModel, Generic[T]):
    status_code: int = Field(default=result_code.SysCont.Success.Code, title="成功状态码", example=200)
    redirect_url: str = Field(default="", title="跳转Url", example=200)
    msg: dict = Success.dict()
    data: T
    timestamp: int = int(time.time() * 1000)


def ok(data: T, msg: ErrMsg = Success, redirect_url=""):
    return ResultMsg[T](data=data, msg=msg.dict(), redirect_url=redirect_url)


def fail(result: result_code.ResultCont, msg: ErrMsg, message: Any = None, redirect_url="", contact=None):
    msg.reason = result.Msg
    msg.text = message or msg.text
    msg.contact = contact or msg.contact
    return ResultMsg[Dict](
        data={}, msg=msg.dict(), redirect_url=redirect_url,
        status_code=result.Code
    )

